/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.presentationappearance;

import org.openaecfm.model.measure.NormalisedRatioMeasure;
import org.openaecfm.model.presentationappearance.ColourRgb;
import org.openaecfm.model.presentationappearance.ReflectanceMethodEnum;
import org.openaecfm.model.presentationappearance.SpecularExponent;
import org.openaecfm.model.presentationappearance.SpecularRoughness;
import org.openaecfm.model.presentationappearance.SurfaceStyleRendering;
import org.openaecfm.model.presentationappearance.SurfaceStyleShading;
import org.openaecfm.spf.io.AbstractEntityInstanceInputImpl;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceOutput;
import org.openaecfm.spf.io.IEntityInstanceUtilInput;
import org.openaecfm.spf.io.IEntityInstanceUtilOutput;
import org.openaecfm.spf.model.DollarParameter;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.EntityInstanceNameParameter;
import org.openaecfm.spf.model.EnumerationParameter;
import org.openaecfm.spf.model.NodeType;
import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.RealParameter;
import org.openaecfm.spf.model.SimpleEntityInstance;
import org.openaecfm.spf.model.TypedParameter;

public final class SurfaceStyleRenderingIO extends
		AbstractEntityInstanceInputImpl<SurfaceStyleRendering> implements
		IEntityInstanceOutput<SurfaceStyleRendering> {
	private IEntityInstanceUtilInput<SurfaceStyleShading> utilInput;
	private IEntityInstanceUtilOutput<SurfaceStyleShading> utilOutput;

	public void setUtilOutput(
			IEntityInstanceUtilOutput<SurfaceStyleShading> utilOutput) {
		this.utilOutput = utilOutput;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String exportSPF(SurfaceStyleRendering pojo,
			IAppDataManager appDataManager) {
		SimpleEntityInstance ei = new SimpleEntityInstance(
				"IFCSURFACESTYLERENDERING", appDataManager.getName(pojo));

		utilOutput.exportSPF(pojo, ei, appDataManager);

		// Set Transparency
		if (pojo.getTransparency() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new RealParameter(pojo.getTransparency()));
		}

		// Set DiffuseColour
		if (pojo.getDiffuseColour() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			if (pojo.getDiffuseColour() instanceof NormalisedRatioMeasure) {
				TypedParameter t = new TypedParameter(
						"IFCNORMALISEDRATIOMEASURE", new RealParameter(
								((NormalisedRatioMeasure) pojo
										.getDiffuseColour()).doubleValue()));
				ei.addParameter(t);
			} else if (pojo.getDiffuseColour() instanceof ColourRgb) {
				ei.addParameter(new EntityInstanceNameParameter(appDataManager
						.getName(pojo.getDiffuseColour())));
			} else {
				throw new RuntimeException("Not Support Class Type!");
			}
		}

		// Set TransmissionColour
		if (pojo.getTransmissionColour() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			if (pojo.getTransmissionColour() instanceof NormalisedRatioMeasure) {
				TypedParameter t = new TypedParameter(
						"IFCNORMALISEDRATIOMEASURE",
						new RealParameter(((NormalisedRatioMeasure) pojo
								.getTransmissionColour()).doubleValue()));
				ei.addParameter(t);
			} else if (pojo.getTransmissionColour() instanceof ColourRgb) {
				ei.addParameter(new EntityInstanceNameParameter(appDataManager
						.getName(pojo.getTransmissionColour())));
			} else {
				throw new RuntimeException("Not Support Class Type!");
			}
		}

		// Set DiffuseTransmissionColour
		if (pojo.getDiffuseTransmissionColour() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			if (pojo.getDiffuseTransmissionColour() instanceof NormalisedRatioMeasure) {
				TypedParameter t = new TypedParameter(
						"IFCNORMALISEDRATIOMEASURE", new RealParameter(
								((NormalisedRatioMeasure) pojo
										.getDiffuseTransmissionColour())
										.doubleValue()));
				ei.addParameter(t);
			} else if (pojo.getDiffuseTransmissionColour() instanceof ColourRgb) {
				ei.addParameter(new EntityInstanceNameParameter(appDataManager
						.getName(pojo.getDiffuseTransmissionColour())));
			} else {
				throw new RuntimeException("Not Support Class Type!");
			}
		}

		// Set ReflectionColour
		if (pojo.getReflectionColour() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			if (pojo.getReflectionColour() instanceof NormalisedRatioMeasure) {
				TypedParameter t = new TypedParameter(
						"IFCNORMALISEDRATIOMEASURE", new RealParameter(
								((NormalisedRatioMeasure) pojo
										.getReflectionColour()).doubleValue()));
				ei.addParameter(t);
			} else if (pojo.getReflectionColour() instanceof ColourRgb) {
				ei.addParameter(new EntityInstanceNameParameter(appDataManager
						.getName(pojo.getReflectionColour())));
			} else {
				throw new RuntimeException("Not Support Class Type!");
			}
		}

		// Set SpecularColour
		if (pojo.getSpecularColour() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			if (pojo.getSpecularColour() instanceof NormalisedRatioMeasure) {
				TypedParameter t = new TypedParameter(
						"IFCNORMALISEDRATIOMEASURE", new RealParameter(
								((NormalisedRatioMeasure) pojo
										.getSpecularColour()).doubleValue()));
				ei.addParameter(t);
			} else if (pojo.getSpecularColour() instanceof ColourRgb) {
				ei.addParameter(new EntityInstanceNameParameter(appDataManager
						.getName(pojo.getSpecularColour())));
			} else {
				throw new RuntimeException("Not Support Class Type!");
			}
		}

		// Set SpecularHighlight
		if (pojo.getSpecularHighlight() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			if (pojo.getSpecularHighlight() instanceof SpecularExponent) {
				TypedParameter t = new TypedParameter("IFCSPECULAREXPONENT",
						new RealParameter(((SpecularExponent) pojo
								.getSpecularHighlight()).doubleValue()));
				ei.addParameter(t);
			} else if (pojo.getSpecularHighlight() instanceof SpecularRoughness) {
				TypedParameter t = new TypedParameter("IFCSPECULARROUGHNESS",
						new RealParameter(
								((SpecularRoughness) pojo
										.getSpecularHighlight()).doubleValue()));
				ei.addParameter(t);
			} else {
				throw new RuntimeException("Not Support Class Type!");
			}
		}

		// Set ReflectanceMethod
		ei.addParameter(new EnumerationParameter(pojo.getReflectanceMethod()));

		return ei.toString();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected SurfaceStyleRendering doCreateObject() {
		return new SurfaceStyleRendering();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void resolveReferenceParameter(EntityInstance ei,
			SurfaceStyleRendering pojo, IAppDataManager appDataManager) {
		utilInput.resolveReferenceParameter(ei, pojo, appDataManager);

		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set DiffuseColour
		Parameter diffusecolour = sei.getParameter(2);
		if (diffusecolour.getNodeType() == NodeType.TYPED_PARAMETER) {
			TypedParameter t = (TypedParameter) diffusecolour;
			RealParameter r = (RealParameter) t.getParameter();
			pojo.setDiffuseColour(new NormalisedRatioMeasure(r.toDouble()));
		}
		if (diffusecolour.getNodeType() == NodeType.ENTITY_INSTANCE_NAME) {
			EntityInstanceNameParameter n = (EntityInstanceNameParameter) diffusecolour;
			Object entity = appDataManager.getObject(n.toInstanceName());
			pojo.setDiffuseColour(entity);
		}

		// Set TransmissionColour
		Parameter transmissioncolour = sei.getParameter(3);
		if (transmissioncolour.getNodeType() == NodeType.TYPED_PARAMETER) {
			TypedParameter t = (TypedParameter) transmissioncolour;
			RealParameter r = (RealParameter) t.getParameter();
			pojo.setTransmissionColour(new NormalisedRatioMeasure(r.toDouble()));
		}
		if (transmissioncolour.getNodeType() == NodeType.ENTITY_INSTANCE_NAME) {
			EntityInstanceNameParameter n = (EntityInstanceNameParameter) transmissioncolour;
			Object entity = appDataManager.getObject(n.toInstanceName());
			pojo.setTransmissionColour(entity);
		}

		// Set DiffuseTransmissionColour
		Parameter diffusetransmissioncolour = sei.getParameter(4);
		if (diffusetransmissioncolour.getNodeType() == NodeType.TYPED_PARAMETER) {
			TypedParameter t = (TypedParameter) diffusetransmissioncolour;
			RealParameter r = (RealParameter) t.getParameter();
			pojo.setDiffuseTransmissionColour(new NormalisedRatioMeasure(r
					.toDouble()));
		}
		if (diffusetransmissioncolour.getNodeType() == NodeType.ENTITY_INSTANCE_NAME) {
			EntityInstanceNameParameter n = (EntityInstanceNameParameter) diffusetransmissioncolour;
			Object entity = appDataManager.getObject(n.toInstanceName());
			pojo.setDiffuseTransmissionColour(entity);
		}

		// Set ReflectionColour
		Parameter reflectioncolour = sei.getParameter(5);
		if (reflectioncolour.getNodeType() == NodeType.TYPED_PARAMETER) {
			TypedParameter t = (TypedParameter) reflectioncolour;
			RealParameter r = (RealParameter) t.getParameter();
			pojo.setReflectionColour(new NormalisedRatioMeasure(r.toDouble()));
		}
		if (reflectioncolour.getNodeType() == NodeType.ENTITY_INSTANCE_NAME) {
			EntityInstanceNameParameter n = (EntityInstanceNameParameter) reflectioncolour;
			Object entity = appDataManager.getObject(n.toInstanceName());
			pojo.setReflectionColour(entity);
		}

		// Set SpecularColour
		Parameter specularcolour = sei.getParameter(6);
		if (specularcolour.getNodeType() == NodeType.TYPED_PARAMETER) {
			TypedParameter t = (TypedParameter) specularcolour;
			RealParameter r = (RealParameter) t.getParameter();
			pojo.setSpecularColour(new NormalisedRatioMeasure(r.toDouble()));
		}
		if (specularcolour.getNodeType() == NodeType.ENTITY_INSTANCE_NAME) {
			EntityInstanceNameParameter n = (EntityInstanceNameParameter) specularcolour;
			Object entity = appDataManager.getObject(n.toInstanceName());
			pojo.setSpecularColour(entity);
		}
	}

	public void setUtilInput(
			IEntityInstanceUtilInput<SurfaceStyleShading> utilInput) {
		this.utilInput = utilInput;
	}

	@Override
	protected void resolveNotReferenceParameter(EntityInstance ei,
			SurfaceStyleRendering pojo) {
		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		utilInput.resolveNotReferenceParameter(ei, pojo);

		// Set Transparency
		Parameter transparency = sei.getParameter(1);
		if (transparency.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) transparency;
			pojo.setTransparency(r.toDouble());
		}

		// Set SpecularHighlight
		Parameter specularhighlight = sei.getParameter(7);
		if (specularhighlight.getNodeType() == NodeType.TYPED_PARAMETER) {
			TypedParameter t = (TypedParameter) specularhighlight;
			RealParameter r = (RealParameter) t.getParameter();
			if (t.getKeyword().equals("IFCSPECULAREXPONENT")) {
				pojo.setSpecularHighlight(new SpecularExponent(r.toDouble()));
			}
			if (t.getKeyword().equals("IFCSPECULARROUGHNESS")) {
				pojo.setSpecularHighlight(new SpecularRoughness(r.toDouble()));
			}
		}

		// Set ReflectanceMethod
		EnumerationParameter method = (EnumerationParameter) sei
				.getParameter(8);
		pojo.setReflectanceMethod(ReflectanceMethodEnum.valueOf(method
				.toEnumString()));
	}
}
